<template>
    <nav class="fixtop">
        <div class="title" @click="goToPage({ name: '首页', url: '/' })">武汉杰创博特自动化科技有限公司</div>

        <!-- 移动端的汉堡菜单 -->
        <div class="hamburger" v-if="isMobile">
            <el-dropdown placement="bottom-start" size="large">
                <el-icon size="30" color="white">
                    <Grid />
                </el-icon>
                <template #dropdown>
                    <el-dropdown-menu>
                        <el-dropdown-item v-for="item in menuList" @click="goToPage(item)">
                            {{ item.name }}
                        </el-dropdown-item>
                    </el-dropdown-menu>
                </template>
            </el-dropdown>
        </div>

        <!-- PC 端菜单 -->
        <div class="link" v-if="!isMobile">
            <div class="menu-item" v-for="item in menuList" :key="item.url">
                <a @click="goToPage(item)">{{ item.name }}</a>
                <!-- 子菜单 -->
                <div class="submenu" v-if="item.name !== '首页'&& item.name !== '智慧管理平台'">
                    <p>子菜单1</p>
                    <p>子菜单2</p>
                    <p>子菜单3</p>
                </div>
            </div>
        </div>
    </nav>
</template>


<script setup>
import { ref, onMounted, onUnmounted } from 'vue';
import { useRouter } from 'vue-router';

const router = useRouter();

// 菜单数据
const menuList = ref([
    { name: '首页', url: '/' },
    { name: '关于我们', url: '/about' },
    { name: '产品与服务', url: '/services' },
    { name: '案例展示', url: '/cases' },
    { name: '新闻资讯', url: '/news' },
    { name: '智慧管理平台', url: 'http://localhost/login' },
]);

// 跳转页面
const goToPage = (item) => {
    if (item.name === '智慧管理平台') {
        window.open('http://localhost/login', '_blank');
    } else {
        router.push(`${item.url}`);
    }
};

// 监听屏幕宽度变化，判断是否为移动端
const isMobile = ref(window.innerWidth <= 800);
const updateIsMobile = () => {
    isMobile.value = window.innerWidth <= 800;
};

onMounted(() => {
    window.addEventListener('resize', updateIsMobile);
});

onUnmounted(() => {
    window.removeEventListener('resize', updateIsMobile);
});
</script>


<style scoped lang="less">
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

.fixtop {
    position: sticky;
    top: 0;
    left: 0;
    width: 100%;
    height: 70px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: rgba(0, 47, 167, 0.9);
    z-index: 1000;
    color: #fff;
    font-size: 16px;

    .title {
        font-family: '宋体';
        font-size: 24px;
        font-weight: bold;
        padding-left: 20px;
        cursor: pointer;
    }

    .link {
        display: flex;

        .menu-item {
            position: relative;
            margin: 0 2vw;
            min-width: 80px;
            text-align: center;
            //padding-bottom: 15px; // 增加触发区域

            a {
                text-decoration: none;
                color: #fff;
                transition: color 0.3s;
            }

            // 🔥 关键：使用 opacity 和 visibility 避免 display:none 问题
            .submenu {
                position: absolute;
                top: 150%; // 紧贴菜单项，避免鼠标间隙
                left: 0;
                background-color: #1943b0;
                padding: 10px;
                border-radius: 5px;
                box-shadow: 0 2px 5px rgba(0, 0, 0, 0.5);
                min-width: 100px;
                opacity: 0;
                visibility: hidden;
                transition: opacity 0.3s ease-in-out, visibility 0.3s ease-in-out;
            }

            &:hover .submenu {
                opacity: 1;
                visibility: visible;
            }
        }
        @media screen and (max-width: 1000px) {
            .menu-item{
                margin: 0 0.2vw;
            }
        }
    }

}

// 移动端样式
@media (max-width: 800px) {
    .link {
        display: none;
        flex-direction: column;
        position: absolute;
        top: 70px;
        right: 0;
        background-color: rgba(0, 0, 0, 0.9);
        width: 100%;
        padding: 10px;
        box-shadow: 0 2px 5px rgba(0, 0, 0, 0.5);
    }

    .hamburger {
        display: flex;
        visibility: visible;
        padding-right: 10px;
    }
}
</style>
